package com.xqboss.apps.service.user;

import com.xqboss.apps.domain.user.UserIdentityPermWrenchPresenter;
import com.xqboss.apps.mapper.user.UserIdentityPermWrenchPresenterMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xqboss.apps.vo.user.HotBeansGiveBeforeVo;
import com.xqboss.common.constant.CacheConstants;
import com.xqboss.common.core.redis.RedisCache;
import com.xqboss.common.exception.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import com.xqboss.chain.domain.chain.dto.BaseRequest;

import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;

/**
 * <p>
 * 扳手转增配置 服务实现类
 * </p>
 *
 * @author 木鱼
 * @since 2024-01-22
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class UserIdentityPermWrenchPresenterService extends ServiceImpl<UserIdentityPermWrenchPresenterMapper, UserIdentityPermWrenchPresenter> {
    @Autowired
    private SysUserHotbeansRecordService sysUserHotbeansRecordService;
    @Autowired
    private RedisCache redisCache;

    public List<UserIdentityPermWrenchPresenter> selectBasicList(BaseRequest request) {
        return baseMapper.selectList(Wrappers.<UserIdentityPermWrenchPresenter>lambdaQuery());
    }

    public UserIdentityPermWrenchPresenter selectByIdBasic(Long id) {
        return baseMapper.selectById(id);
    }

    public boolean saveBasic(UserIdentityPermWrenchPresenter record) {
        return baseMapper.insert(record) > 0;
    }

    public boolean updateBasic(UserIdentityPermWrenchPresenter record) {
        return baseMapper.updateById(record) > 0;
    }

    public boolean removeBasic(Long id) {
        return baseMapper.deleteById(id) > 0;
    }

    /**
     * 根据身份获取扳手转增配置
     *
     * @param identityId
     * @return
     */
    public UserIdentityPermWrenchPresenter getWrenchConfig(Long identityId) {
        UserIdentityPermWrenchPresenter one = this.lambdaQuery().eq(UserIdentityPermWrenchPresenter::getIdentityId, identityId).one();
        return one == null ? new UserIdentityPermWrenchPresenter() : one;

    }

    /**
     * 根据身份id查询是否有扳手转增权限
     * @param userId
     * @return
     */
    public boolean getIsOpen(Long userId) {
        Long identityId = sysUserHotbeansRecordService.getUserIdentity(userId);
        UserIdentityPermWrenchPresenter one = this.lambdaQuery().eq(UserIdentityPermWrenchPresenter::getIdentityId, identityId).last("limit 1").one();
        return (one==null?0:one.getIsOpen())!=0;
    }


    /**
     * 扳手转增前置信息
     * @param identityId
     * @param userId
     * @return
     */
    public HotBeansGiveBeforeVo wrenchGiveBefore(Long identityId, Long userId) {
        UserIdentityPermWrenchPresenter one = this.lambdaQuery().eq(UserIdentityPermWrenchPresenter::getIdentityId, identityId).one();
        if (one == null){
            log.error("该身份没有配置扳手转增权限");
            throw new ServiceException("该身份没有扳手转增权限");
        }
        HotBeansGiveBeforeVo vo = new HotBeansGiveBeforeVo();
        vo.setMaxDailyTimes(one.getMaxDailyTimes());
        vo.setFeeRate(one.getFeeRate());
        vo.setMaxSingleAmount(one.getMaxSingleAmount());
        vo.setMinSingleAmount(one.getMinSingleAmount());
        // 从redis获取用户转增使用次数,如果没有 则为0
        Map<String, Integer> map = redisCache.getCacheMap(CacheConstants.USER_WRENCH_COUNT_ALL_KEY);
        // 热豆使用次数
        Integer integer = map.get(CacheConstants.USER_WRENCH_COUNT_KEY + userId);
        if (integer == null) {// 用户当日没有进行过转增,使用次数应为0
            vo.setHitCount(0);
        } else {
            vo.setHitCount(integer);
        }
        return vo;
    }
}